package com.dfrz.stockMarket.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dfrz.stockMarket.bean.UserBean;
import com.dfrz.stockMarket.model.StocksModel;
import com.dfrz.stockMarket.model.UserOrderModel;
import com.dfrz.stockMarket.model.UserStocksModel;
import com.dfrz.stockMarket.service.StockService;
import com.dfrz.stockMarket.service.UserService;

import javafx.scene.chart.PieChart.Data;

public class BayCtrl extends BaseController{
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		System.out.println("开心");
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		Integer costPrice = Integer.parseInt(req.getParameter("costPrice"));
		Integer Codeid= Integer.parseInt(req.getParameter("stockCode"));
		Integer stockQuantity = Integer.parseInt(req.getParameter("stockQuantity"));
		String Codename = req.getParameter("Codename");
		System.out.println("Codename1--"+Codename);
		
		UserBean userBean = getSessionUserBean(req);
		int userid=userBean.getUserInfoModel().getUserID();
		String username=userBean.getUserInfoModel().getUserName();
		StockService stockservice =new StockService();
		List<StocksModel> list = null;
		UserService service = new UserService();
		try {
			list = (List<StocksModel>) stockservice.getAllUserStockCode(userid).getMsg();
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		UserStocksModel userStocksModel1=null;
		for(Object obj:list){
			int sto=((UserStocksModel)obj).getStockCode();
			System.out.println("sql.getstockcode--"+sto);
			System.out.println("Codeid--"+Codeid);
			System.out.println("Codename2--"+Codename);
			
			if(sto==Codeid){
				Date data=new Date();
				UserOrderModel userorder=new UserOrderModel(userid, Codeid,Codename, 1, stockQuantity, costPrice, data);
					try {
						service.saveUserOrderModel(userorder);
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
		}
		
	
	}
}
